<?php
/**
 * A TestController is an instance of Controller (application.components). 
 *
 * Controller is an instance of CController (system.web).
 * CController manages a set of actions which deal with the corresponding user requests. 
 * Through the actions, CController coordinates the data flow between models and views. 
 *
 * Eduow :  International system for education (http://www.eduow.com)
 * Copyright 2010, Nikolay Stoitsev and Ivailo Danev
 *
 * Licensed under GNU General Public License version 2
 * Redistributions of files must retain the above copyright notice.
 *
 * @filesource
 * @copyright     Copyright 2010
 * @link          http://eduow.com Eduow Project
 * @package       eduow
 * @subpackage    application.controllers
 * @inheritance   class TestController » Controller »CController » CBaseController » CComponent
 * @implements    
 * @since         Eduow v 0.0.3
 * @license       http://www.gnu.org/licenses/gpl-2.0.html GNU General Public License version 2
 *                http://bulgaria.sourceforge.net/prava/gplbg.html GNU General Public License version 2
 */


class TestController extends Controller
{
	/**
	 * @var string the default layout for the views. Defaults to 'column2', meaning
	 * using two-column layout. See 'protected/views/layouts/column2.php'.
	 */
	public $layout='main';

	/**
	 * @var CActiveRecord the currently loaded data model instance.
	 */
	private $_model;

	/**
	 * @return array action filters
	 */
	public function filters()
	{
		return array(
			'accessControl', // perform access control for CRUD operations
		);
	}

	/**
	 * Specifies the access control rules.
	 * This method is used by the 'accessControl' filter.
	 * @return array access control rules
	 */
	public function accessRules()
	{
		return array(
			'view, index, last'=>array('*'),
			'mine'=>array(Group::USER, 'min'),
			'create, update, delete, admin' => array(Group::TEACHER, 'min'),
		);
	}

	/**
	 * Displays a particular model.
	 */
	public function actionView()
	{
		$this->render('view',array(
			'model'=>$this->loadModel(),
		));
	}

	/**
	 * Creates a new model.
	 * If creation is successful, the browser will be redirected to the 'view' page.
	 */
	public function actionCreate()
	{
		$model=new test;

		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);

		if(isset($_POST['test']))
		{
			$model->attributes=$_POST['test'];
			if($model->save())
				Yii::app()->user->setFlash('model','Your test have been published succesfully. Thank you!');
		}

		$this->render('create',array(
			'model'=>$model,
		));
	}

	/**
	 * Updates a particular model.
	 * If update is successful, the browser will be redirected to the 'view' page.
	 */
	public function actionUpdate()
	{
		$model=$this->loadModel();

		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);

		if(isset($_POST['test']))
		{
			$model->attributes=$_POST['test'];
			if($model->save())
				Yii::app()->user->setFlash('model','Your test have been updated succesfully. Thank you!');
		}

		$this->render('update',array(
			'model'=>$model,
		));
	}

	/**
	 * Deletes a particular model.
	 * If deletion is successful, the browser will be redirected to the 'index' page.
	 */
	public function actionDelete()
	{
		if(Yii::app()->request->isPostRequest)
		{
			// we only allow deletion via POST request
			$this->loadModel()->delete();

			// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
			if(!isset($_POST['ajax']))
				$this->redirect(array('index'));
		}
		else
			throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
	}

	/**
	 * Lists all models.
	 */
	public function actionIndex()
	{
		$dataProvider=new CActiveDataProvider('test');
		$this->render('index',array(
			'dataProvider'=>$dataProvider,
		));
	}

	/**
	 * Manages all models.
	 */
	public function actionAdmin()
	{
		$model=new test('search');
		if(isset($_GET['test']))
			$model->attributes=$_GET['test'];

		$this->render('admin',array(
			'model'=>$model,
		));
	}

	/**
	 * Lists own models.
	 */
	public function actionMine()
	{
		$dataProvider=new CActiveDataProvider('test',array(
		'criteria'=>array(
        		'condition'=>'author = '.Yii::app()->user->id,
			),
    		'pagination'=>array(
       			'pageSize'=>40,
    			),
		));
		$this->render('index',array(
			'dataProvider'=>$dataProvider,	
			'message'=>'Your '
		));
	}

	/**
	 * Lists last created models.
	 */
	public function actionLast()
	{
		$dataProvider=new CActiveDataProvider('test',array(
		'criteria'=>array(
        		'condition'=>'Test1 = :t1 OR Test1 = :t2',
			'params'=>array(':t1'=>'test ','t2'=>'fgh'),
        		'limit'=>10,
		),
		'pagination'=>false,
		));
		$this->render('index',array(
			'dataProvider'=>$dataProvider,	
			'message'=>'Last 10 '
		));
	}

	/**
	 * Returns the data model based on the primary key given in the GET variable.
	 * If the data model is not found, an HTTP exception will be raised.
	 */
	public function loadModel()
	{
		if($this->_model===null)
		{
			if(isset($_GET['id']))
				$this->_model=test::model()->findbyPk($_GET['id']);
			if($this->_model===null)
				throw new CHttpException(404,'The requested page does not exist.');
		}
		return $this->_model;
	}

	/**
	 * Performs the AJAX validation.
	 * @param CModel the model to be validated
	 */
	protected function performAjaxValidation($model)
	{
		if(isset($_POST['ajax']) && $_POST['ajax']==='test-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
	}
}
